package com.ruoyi.system.service;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ruoyi.system.domain.vo.AdapterConstant;
import com.ruoyi.system.domain.vo.MessageTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @create: 2020-06-03 14:25
 **/
@Service
public class SmsSerivce {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * 调用阿里云发送短信
     *
     * @param messageTemplate 使用的消息模板
     * @param mobile          手机号、
     * @param codeArray       参数
     * @return
     */
    public boolean send(MessageTemplate messageTemplate, String mobile,String accessKeyId,String accessSecret,String... codeArray) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", AdapterConstant.ADAPTER_SMS_SIGN);
        request.putQueryParameter("TemplateCode", messageTemplate.getCode());
        String param = messageTemplate.getParam();
        for (int i = 0; i < codeArray.length; i++) {
            param = param.replace("$$", codeArray[i]);
        }
        request.putQueryParameter("TemplateParam", param);
        try {
            logger.debug("调用短信发送接口：手机号[{}]，模板编号[{}]，发送内容[{}]", mobile, messageTemplate.getCode(), param);
            CommonResponse response = client.getCommonResponse(request);
            JSONObject json = JSONObject.parseObject(response.getData());
            if (json != null && "OK".equals(json.getString("Code"))) {
                logger.debug("调用短信发送接口：手机号[{}]，短信发送成功", mobile);
            } else {
                logger.error("调用发送短信接口失败，返回信息:{}", response.getData());
                return false;
            }
            logger.debug("调用短信发送接口返回参数：{}", response.getData());
            return true;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }



/*    public boolean QuerySendDetails(Long currentPage,Long pageSize,String phoneNumber,String SendDate) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("QuerySendDetails");
        request.putQueryParameter("CurrentPage","1");
        request.putQueryParameter("PageSize","1");
        request.putQueryParameter("PhoneNumber", phoneNumber);
        request.putQueryParameter("SendDate", DateTimeUtil.formatDateTimetoString(new Date(),"yyyyMMdd"));

        try {
            logger.debug("调用短信查询：手机号[{}]，", phoneNumber);
            CommonResponse response = client.getCommonResponse(request);
            JSONObject json = JsonKit.parseJSON(response.getData());
            if (json != null && "OK".equals(json.getString("Code"))) {

            } else {
                logger.error("调用发送短信接口失败，返回信息:{}", response.getData());
                return false;
            }
            logger.debug("调用短信发送接口返回参数：{}", response.getData());
            return true;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }*/
}
